﻿//当前所在的页数
var pg_down_index;
//顶部菜单的高度
var topHeight = 86; 
 //快捷菜单的行数
var max_line;
//增加Z_index的一个常数
var z_index_const = 1;
var menu_z_index = 9000;
var north_head, north_height;//头部对象，和头部高度
var center_content,center_height;//内容对象，和内容高度
var south_foot,south_height;//页脚对象，和页脚高度
var left_menu,left_height; //左侧菜单对象，和对象高度
var sc_height = $(window).height();
$(document).ready(function() {
    //初始化内容主体部分
    try {
        north_head = $('#north_head');
        center_content = $('#center_content');
        south_foot = $('#south_footer');
        left_menu = $('#left_menu');

         south_height = south_foot.outerHeight();
         north_height = north_head.outerHeight();
         center_height = sc_height - north_height - south_height;
        //初始化容器
        SetPartsStyle();
        //导航菜单打开关闭时间
        NavigateMenuOpenEvent();
        //初始化快捷菜单
        InitMenu('#menu_eara');
        //导航菜单初始化
        //        while (true) {
        //            var rBool = NavigateMenuInit('ePlanet');
        //            if (!rBool) {
        //                break;
        //            }
        //        }
//        GetNavigateContent('');
    } catch (er) { }

});

//设置各部分的样式
function SetPartsStyle() {
    //内容部分的高度
    center_content.css('height', center_height - 2);
    //菜单的高度
    left_menu.css('height', center_height-2);
}

//导航菜单打开关闭时间
function NavigateMenuOpenEvent() {
    $('.foot-btn-menu').bind('click', function() {
    $('#left_menu').slideToggle('slow');
});
}


//导航菜单初始化
function NavigateMenuInit(rootId) {
    var menu = $('.easyui-accordion');
    var _parentId = rootId;
    if (menu.length > 0) {
        $.ajax({
            type: "POST",
            url: 'Views/SystemLogin/LoadMenuService.asmx/GetNavigateMenuData',
            data: { parent_Id: _parentId },
            success: function(result) {
                //添加分类菜单
                if (result.toString().toLowerCase() == "empty")//如果返回的是字符串"Empty"，表示是叶节点，不再继续加载
                {
                    return false;
                }
                else if (result.toString().indexOf('<html>') > 0) {//包含<html>标签的是表示错误页面，不加载
                    return false;
                }
                //否则继续加载
                else {
                    //一级菜单作为导航菜单加载
                    var list = eval(result);
                    $(list).each(function(i, item) {
                        var shtml = "";
                        //一直加载到顶级
                        //                        while (true) {
                        //                        }
                        var shtml = GetNavigateContent(item.menuId);
                        menu.append(shtml);
                        return true;
                        //                        //加载下级
                        //                        NavigateMenuInit();
                        //加载导航条(判断是否是一级导航菜单)
                        //                        if (item.parentMenuId == rootId) {
                        //                            menu.accordion('add', {
                        //                                title: item.menuText,
                        //                                content: shtml
                        //                            });
                        //                        }
                    });
                }
            }
        });      
    }
}

//加载菜单内容
/*menuId  表示菜单ID*/
function GetNavigateContent(_parentId) {
    var menu = $('.treemenu');
    menu.append("<ul class=\"menu_lv1\"></ul>");
    $.ajax(
    {
        type: "POST",
        url: 'Views/SystemLogin/GetCommanMenuById.ashx?parentId=' + _parentId,
        success: function(result) {
            //添加分类菜单
            if (result.toString().toLowerCase() == "empty")//如果返回的是字符串"Empty"，表示是叶节点，不再继续加载
            {
                return false;
            }
            else if (result.toString().indexOf('<html>') > 0) {//包含<html>标签的是表示错误页面，不加载
                return false;
            }
            else {
                var list = eval(result);
                var _data = [];
                $(list).each(function(i, item) {
                    var treenode = new TreeNodeData();
                    treenode.id = item.id;
                    treenode.pid = item.pid;
                    treenode.text = item.text;
                    if (treenode.pid != "0") {
                        treenode.isexpand = false;
                    }
                    else {
                        treenode.isexpand = true; 
                    }
                    _data.push(treenode);
                });

                var ajaxTree = menu.find('.menu_lv1');
                ajaxTree.ligerTree({
                    checkbox: false,
                    data: _data,
                    idFieldName: 'id',
                    parentIDFieldName: 'pid'
                });
            }
        }
    });  
}

function TreeNodeData() {
    this.id;
    this.pid;
    this.text;
    this.isexpand;
}



 function onBeforeExpand(note)
        {
           alert(note);
        }
        function onExpand(note)
        { 
        alert(note);
        }


function LeftMenuBindEvent(elemId) {
    var Btn = $("#" + elemId).find('span');
    var Img = $("$"+elemId).find('img');
    Btn.bind('click', function() { 
    
    });
}


var menu_Eara;
function InitMenu(elem) {
    menu_Eara = $(elem);
    $.ajax({
        type: "POST",
        url: 'Views/SystemLogin/LoadMenuService.asmx/GetMenuData',
        success: function(result) {
            if (result.toString().indexOf('<html>') > 0) {//包含<html>标签的是表示错误页面，不加载
                return;
            }
            else {
                pg_down_index = 1;
                menu_Eara.append("<ul></ul>");
                var list = eval(result);
                $(list).each(function(i, item) {
                    var shtml = "<li id=" + item.menuId + "><a href='javascript:void(0)'><img src='" + item.menuImage + "' alt='" + item.menuText + "' height='60px' width='60px'/><span>" + item.menuText + "</span></a></li>";
                    menu_Eara.find('ul').append(shtml);
                    max_line = get_Max_Line(i, 6);
                    //初始化事件
                    var elem = menu_Eara.find("#" + item.menuId + "");
                    //鼠标动到元素上事件
                    MenuMouseOn(elem);
                    //鼠标移开元素事件
                    MenuMouseOut(elem);
                    //点击元素事件
                    MenuClick(elem, item.menuUrl);
                });
                //上翻页事件
                Pg_down();
                //下翻页事件
                Pg_Up();
            }
        }
    });
}

//鼠标动到元素上事件
function MenuMouseOn(elem) {
    var _this = elem;
    _this.bind('mouseover', function() {
        _this.find('img').css('height', 64).css('width', 64);
        _this.css('margin', '2px');
    });
}
//鼠标移开元素事件
function MenuMouseOut(elem) {
    var _this = elem;
    _this.bind('mouseout', function() {
        _this.find('img').css('height', 60).css('width', 60);
        _this.css('margin', '4px');
    });

}


//点击元素事件
function MenuClick(elem, url) {

    var panel_height = center_height - 5;
    var _this = elem;

    _this.bind('click', function() {
        //    var w = top.window.document.frames('content_frame');
        //        w.location.href = url;
        var _i = z_index_const;
        var id = _this.attr('id');
        var content = $('#center_content');
        var _panelDiv = $("#" + id + "_panel");
        if (_panelDiv.length == 0) {
            content.append("<div id='" + id + "_panel'></div>");
            _panelDiv = $("#" + id + "_panel");
        }
        //获得标题
        var p_title = _this.find('span').html();
        //Iframe地址
        var p_url = url;
        //打开Panel
        OpenPanel(_panelDiv, {
            title: p_title,
            top: north_height,
            z_index: menu_z_index + _i,
            url: p_url,
            //最大化事件
            onMax: function() {
                content.css('height', north_height + center_height - 2);
                _panelDiv.panel('resize', function() {
                    height: north_height + center_height - 5
                });
            },
            //最小化事件
            onMin: function() {
                //创建一个任务栏按钮
            var btnMenu = $('#bottom_menu_eara').find("#" + id); 
                if (btnMenu.length == 0)//如果不存在，则创建一个，如果存在，则缩小按钮
                {
                    createTaskButton(id, p_title, _this.find('img').attr('src'));
                }
                else {
                    ChangeTaskButtonStyle(btnMenu);
                }
            },
            //恢复状态事件
            onRestore: function() {
                content.css('height', center_height - 2);
                _panelDiv.panel('resize', function() {
                    height: center_height - 5
                });
            },
            //关闭事件
            onClose: function() {
                //移除一个任务栏按钮
                destoryTaskButton(_this.attr('id'));
            }
        });
        z_index_const++;
    });
    // '<iframe id="content_frame" src="http://www.baidu.com" style="height: 100%; width:100%;" frameborder="0"  scrolling="yes"></iframe>'
}

//在elem中创建一个Panel
//初始化z_index 才能创建多个panel
//除了Elem，其余都是可选参数elem,title, height,width,top,left, url, z_index
function OpenPanel(elem,opnions) {
    var _title = opnions.title || "New Panel";
    var _height = opnions.height || "100%";
    var _width = opnions.width || "100%";
    var _url = opnions.url || "";
    var _z_index = opnions.z_index || 0;
    var _top = opnions.top || 0;
    var _left = opnions.left || 0;
    var style = {};
    if (_z_index != 0) {
        style = { 'z-index': _z_index, 'position': 'absolute' };
    }

    elem.panel({
    title: _title,
        height: _height,
        width: _width,
        top: _top,
//        collapsible: true,
        minimizable: true,
        maximizable: true,
        closable: true,
        fit: true,
        style: style,
        onMaximize: opnions.onMax || function() { },
        onRestore:opnions.onRestore ||function(){},
        onMinimize: opnions.onMin || function() { },
        onClose:opnions.onClose  || function(){},
        content: "<iframe id='content_frame' src='" + _url + "' style='height: 100%; width:100%;border: medium none; background-color: transparent' frameborder='0'  cellspacing='0' border='0' scrolling='yes'></iframe>"
    }).panel('open');   
}

//创建任务栏按钮
function createTaskButton(id,title,imgurl) {
    //创建的位置，在bottom_menu_eara这个Div中。和菜单按钮一起
    var bottom_menu = $('#bottom_menu_eara');
    var shtml = "<div id='"+id+"' style='float: left;'><button class='jq-button btn-icon' title='" + title + "'><img alt='' style=' text-align:center; verticalalign:middle' src='" + imgurl + "'></button></div>";
    bottom_menu.append(shtml);
    var btnMenu = bottom_menu.find("#" + id);
    btnMenu.bind('click', function() {
        //已经点击的按钮样式恢复
        var p_id = bottom_menu.find('.btn-icon-active').parent().attr('id');
        $("#" + p_id + "_panel").parent().css('display','none');
        bottom_menu.find('.btn-icon-active').addClass('btn-icon').removeClass('btn-icon-active');
        //        //相应的Panel隐藏
        //        $("#" + b_id + "_panel").panel('close');
        //新点击的按钮增加样式
        btnMenu.find('.btn-icon').addClass('btn-icon-active').removeClass('btn-icon');
        //打开页面
        var _panel = $("#" + id + "_panel");
        //        var opnoins = _panel.panel('options');
        //        opnoins.style = { 'z-index': menu_z_index + z_index_const, 'position': 'absolute' };
        //通过Panel找到父容器，显示父容器
        _panel.parent().fadeIn(500);
        z_index_const++;
    });
}

//改变按钮大小
function ChangeTaskButtonStyle(taskElem) {
    taskElem.find('.btn-icon-active').addClass('btn-icon').removeClass('btn-icon-active');
}


//消去一个任务栏按钮
function destoryTaskButton(id) {
    var bottom_menu = $('#bottom_menu_eara');
    var btnMenu = bottom_menu.find("#" + id);
    //移除该DOM元素，包括内容
    btnMenu.remove();
}

//向下翻页事件
function Pg_down() {
    $('#pg_down_btn').bind('click', function() {
        //确保点击下一页的时候有菜单，判断下一行有没有菜单
        if (pg_down_index < max_line) {
            var _top = menu_Eara.css('top').replace('px', '');
            _top = parseInt(_top) - topHeight;
            menu_Eara.animate({ top: "" + _top + "px" }, 800);
            pg_down_index++;
        }
    });
}

//向上翻页事件
function Pg_Up() {
    $('#pg_up_btn').bind('click', function() {
        //确保点击上一页的时候有菜单，判断下一行有没有菜单
        if (pg_down_index > 1) {
            var _top = menu_Eara.css('top').replace('px', '');
            _top = parseInt(_top) + topHeight;
            menu_Eara.animate({ top: "" + _top + "px" }, 800);
            pg_down_index--;
        }
    });
}

//相除取整，有余数则加1
function get_Max_Line(num1, num2) {
    var max_int = parseInt(num1 / num2);
    //防止输入错误数字
    if (isNaN(max_int)) {
        return 0;
    }
    else {
        return (num1 % num2) > 0 ? max_int + 1 : max_int;
    }
}
